package a66weding.com.jiehuntong.widgets.popuwindows;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.AddFriendsActivity;
import a66weding.com.jiehuntong.inner.OnClickItem;


public class PopupWindowUtils {
	private  TextView scan_tv;
	private TextView add_tv ;
	private Context ctx;//上下文->获取xml view对象时使用
	private PopupWindow pop;
	private OnClickItem  onClickItem;

	public void setOnClickItem(OnClickItem onClickItem) {
		this.onClickItem = onClickItem;
	}

	public PopupWindowUtils(Context ctx){
		this.ctx = ctx;
	}

	//view->popupwindow弹出时相对的控件
	public void PopShowDown(View view){
		if(pop == null){
			//popupwindow要显示的view
			View popView = View.inflate(ctx, R.layout.title_popup, null);
			/**
			 * contentView->要显示的界面
			 * width->显示界面的宽度
			 * height->显示界面高度
			 * focusable-> 是否有焦点
			 * */
			pop = new PopupWindow(popView, 400, 200, true);
			//BitmapDrawable->Bitmap转化为Drawable
			pop.setBackgroundDrawable(new BitmapDrawable());

			scan_tv = (TextView) popView.findViewById(R.id.scan_tv);
			add_tv = (TextView) popView.findViewById(R.id.add_tv);
			scan_tv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onClickItem.click(1);
					pop.dismiss();
				}
			});
			/**
			 * 输入手机号添加朋友;
			 */
			add_tv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent  = new Intent(ctx, AddFriendsActivity.class);
					ctx.startActivity(intent);
					pop.dismiss();
				}
			});
		}
		//显示pop->showAsDropDown->在相对控件anchor下边
		//xoff->X轴偏移
		//yoff->Y轴偏移
		//
		pop.showAsDropDown(view, (view.getWidth()-100)/2, 0);
	}

	//左侧
	public void ShowPopupLeft(View view){
		if(pop == null){
			View popView = View.inflate(ctx, R.layout.title_popup, null);
			pop = new PopupWindow(popView, 100, 50, true);
			pop.setBackgroundDrawable(new BitmapDrawable());
		}
		int []location = new int[2];
		//location[0]:X轴坐标;
		//location[1]:Y轴坐标
		view.getLocationOnScreen(location);//获取控件在屏幕上x，y位置
		pop.showAtLocation(view, Gravity.NO_GRAVITY, (location[0]-100), location[1]);
	}

	//右侧
	public void ShowPopupRight(View view){
		if(pop == null){
			View popView = View.inflate(ctx, R.layout.title_popup, null);
			pop = new PopupWindow(popView, 100, 50, true);
			pop.setBackgroundDrawable(new BitmapDrawable());
		}
		int []location = new int[2];
		//location[0]:X轴坐标;
		//location[1]:Y轴坐标
		view.getLocationOnScreen(location);//获取控件在屏幕上x，y位置
		pop.showAtLocation(view, Gravity.NO_GRAVITY,
				(location[0]+view.getWidth()), location[1]);
	}


	//上方
	public void ShowPopupTop(View view){
		if(pop == null){
			View popView = View.inflate(ctx, R.layout.title_popup, null);
			pop = new PopupWindow(popView, 100, 50, true);
			pop.setBackgroundDrawable(new BitmapDrawable());
		}
		int []location = new int[2];
		//location[0]:X轴坐标;
		//location[1]:Y轴坐标
		view.getLocationOnScreen(location);//获取控件在屏幕上x，y位置
		pop.showAtLocation(view, Gravity.NO_GRAVITY,
				location[0], (location[1]-50));
	}

	//prg
	public void ShowPopupPrg(View view){
		if(pop == null){
			View popView = View.inflate(ctx, R.layout.title_popup, null);
			pop = new PopupWindow(popView, 100, 100, true);
			pop.setBackgroundDrawable(new BitmapDrawable());
		}
		int width;
		int height;
		//获取窗口管理器
		WindowManager manager = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
		width = manager.getDefaultDisplay().getWidth();
		height = manager.getDefaultDisplay().getHeight();
		pop.showAtLocation(view, Gravity.NO_GRAVITY, (width - 100)/2, (height - 100)/2);
	}

}
